package top.lyjwn.todo.resources.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import top.lyjwn.todo.aop.token.ExemptToken;
import top.lyjwn.todo.aop.token.Token;
import top.lyjwn.todo.common.template.R;
import top.lyjwn.todo.resources.data.FileResource;
import top.lyjwn.todo.resources.service.FileResourceService;
import top.lyjwn.todo.resources.util.FileUtil;

import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

;

/**
 * 文件资源控制器
 * 
 * @author luoyujie
 *
 */
@RestController
@RequestMapping("/resource/file")
public class FileResourceController {

	@Autowired
	private FileResourceService fileResourceService;

	@Autowired
	private Token token;

	@Autowired
	private HttpServletResponse response;

	private final static Logger log = LoggerFactory.getLogger("error");


//	@ExemptToken
//	@RequestMapping(value = "/upload", method = RequestMethod.POST)
//	public ResponseTemplate upload(CommonsMultipartFile file) {
//		return fileResourceService.save("test", true, "test", file.getBytes());
//	}


	/**
	 * 保存文件资源
	 * @param referToUsers
	 * @param isPublic
	 * @param fileName
	 * @param fileResource
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R save(@RequestParam(required = false) String referToUsers,
				  @RequestParam Boolean isPublic, @RequestParam String fileName, @RequestBody byte[] fileResource) {
		return fileResourceService.save(referToUsers, isPublic, fileName, fileResource);
	}

	/**
	 * 保存文件资源列表
	 * @param referToUsers
	 * @param toPublic
	 * @param fileResources
	 * @return
	 */
	@RequestMapping(value = "/save/list", method = RequestMethod.POST)
	public R save(@RequestParam(required = false) String referToUsers,
				  @RequestParam Boolean toPublic, @RequestBody List<FileResource> fileResources) {
		return fileResourceService.save(referToUsers, toPublic, fileResources);
	}

	/**
	 * 根据文件名访问资源
	 *
	 * @throws IOException
	 *
	 */
	@ExemptToken
	@RequestMapping(value = "/{resources}", method = RequestMethod.GET)
	public void getResources(@PathVariable("resources") String fileName)
			throws IOException {
		R<FileResource> byName = fileResourceService.findByName(new FileUtil.OnFileOut() {
			@Override
			public void out(byte[] bytes) throws IOException {
					response.getOutputStream().write(bytes);
			}

		}, fileName);

		if (!byName.isDone()){
			byte[] bytes = FileCopyUtils.copyToByteArray(new ClassPathResource("not_permission.png").getInputStream());
			response.getOutputStream().write(bytes);
		}
	}

	/**
	 * 更新资源
	 * 
	 * @param fileResource
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public R update(FileResource fileResource) {
		return fileResourceService.update(fileResource);

	}

	/**
	 * 更新资源
	 * 
	 * @param name 文件名
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public R delete(String name) {
		return fileResourceService.delete(name);
	}


}
